<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/9/21
 * Time: 15:03
 */

namespace lib\common;

use lib\exception\ParamsException;
use think\Validate;

class CommonValidator extends Validate
{
    public function goCheck (?array $data = null, string $scene = null): bool {
        $params = empty($data) ? request()->all() : $data;
        $valid = empty($scene) ? $this->check($params) : $this->scene($scene)->check($params);
        if (!$valid) {
            throw new ParamsException(['message' => $this->error]);
        }
        return true;
    }

    /**
     * 验证身份证号
     * @param string $idcard
     * @return bool
     */
    protected function idCard (string $idcard): bool {
        // 只能是18位
        if (strlen($idcard) != 18) {
            return false;
        }
        // 取出本体码
        $idcardBase = substr($idcard, 0, 17);
        // 取出校验码
        $verifyCode = substr($idcard, 17, 1);
        // 加权因子
        $factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        // 校验码对应值
        $verifyCodeList = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
        // 根据前17位计算校验码
        $total = 0;
        for ($i = 0; $i < 17; $i++) {
            $total += substr($idcardBase, $i, 1) * $factor[$i];
        }
        // 取模
        $mod = $total % 11;
        // 比较校验码
        return $verifyCode == $verifyCodeList[$mod];
    }

    /**
     * 验证手机号
     * @param string $value
     * @return bool
     */
    protected function mobileNumber (string $value): bool {
        $rule = '^1[[3-9][0-9]\d{8}$^';
        $result = preg_match($rule, $value);
        if ($result) {
            return true;
        } else {
            return false;
        }
    }

    protected function emailAddress ($value): bool {
        $regex = '/^([A-Za-z0-9_\-\.\x80-\xff])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,8})$/';
        $result = preg_match($regex, $value);
        return $result > 0;
    }

    /**
     * 验证电话号码
     * @param string $value
     * @return bool
     */
    protected function telNumber (string $value): bool {
        $mobile = '^1[[3-9][0-9]\d{8}$^';
        $tel = '/^(?:0[1-9][0-9]{1,2}-)?[2-8][0-9]{6,7}$/';
        $tel400 = '/^400[0-9]{7}/';
        $tel800 = '/^800[0-9]{7}/';
        if (preg_match($mobile, $value) || preg_match($tel, $value) || preg_match($tel, $value) || preg_match($tel400, $value) || preg_match($tel800, $value)) {
            return true;
        }
        return false;
    }

    /**
     * 验证银行账户
     * @param string $value
     * @return bool
     */
    protected function bankAccount (string $value): bool {
        $rule = '/^([1-9])(\d{8,22})$/';
        if (preg_match($rule, $value)) {
            return true;
        }
        return false;
    }

    /**
     * 验证某个参数是否是大于0的正整数
     * @param mixed $value
     * @return bool
     */
    protected function postive ($value): bool {
        return (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0);
    }

    protected function long ($value): bool {
        return is_numeric($value) && is_int($value + 0);
    }
}
